package com.czn.controller.admin;

import com.czn.entity.Hospitalization;
import com.czn.service.IHospitalizationService;
import com.czn.service.IPatientService;
import com.czn.utils.ExcelUtil;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ChenZeNan
 * @create 2021-04-23 15:56
 */
@Controller
@RequestMapping("/admin/hospitalization")
public class AdminHospitalizationController {
    @Autowired
    private IHospitalizationService hospitalizationService;
    @Autowired
    private IPatientService patientService;

    /**
     * 分页/模糊查询住院信息
     *
     * @param queryVo
     * @param model
     * @return
     */
    @RequestMapping("/manage")
    public String hospitalizationManage(QueryVo queryVo, Model model) {
        PageQuery<Hospitalization> page = hospitalizationService.findHospitalApplicationList(queryVo);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", queryVo.getPageNo());
        model.addAttribute("patientName", queryVo.getPatientName());
        model.addAttribute("intime", queryVo.getInTime());
        model.addAttribute("path", "/admin/hospitalization/manage");
        StringBuilder sb = new StringBuilder();
        if (queryVo.getPatientName() != null) {
            sb.append("&patientName=" + queryVo.getPatientName());
        }
        if (queryVo.getInTime() != null) {
            sb.append("&intime=" + queryVo.getInTime());
        }
        model.addAttribute("params", sb.toString());
        return "admin/hospitalizationManage";
    }

    /**
     * 跳转到新增住院页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("hospitalization", new Hospitalization());
        model.addAttribute("patients", patientService.findAllPatient());
        return "admin/hospitalizationForm";
    }

    /**
     * 跳转到住院记录修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        Hospitalization hospitalization = hospitalizationService.findHospitalizationById(id);
        model.addAttribute("hospitalization", hospitalization);
        model.addAttribute("patients", patientService.findAllPatient());
        return "admin/hospitalizationForm";
    }

    /**
     * 新增住院信息
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Hospitalization hospitalization) {
        ResponseResult result = hospitalizationService.addHospitalization(hospitalization);
        return result;
    }

    /**
     * 编辑住院信息
     *
     * @param hospitalization
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Hospitalization hospitalization) {
        ResponseResult result = hospitalizationService.editHospitalization(hospitalization);
        return result;
    }

    /**
     * 删除住院信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = hospitalizationService.deleteHospitalizationById(id);
        return result;
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        List<Hospitalization> hospitalizations = hospitalizationService.findAll();
        ExcelUtil.exportExcel(hospitalizations, "住院记录单", "住院记录", Hospitalization.class, "住院记录.xls", response);
    }

}
